import wx
import MainAppHandler

USE_BUFFERED_DC = True # Can be toggled on/off

class CanvasHandler(wx.EvtHandler):

   def __init__(self, mainAppHandler):
      wx.EvtHandler.__init__(self)
      # Get information about the canvas
      # Note, at the time that this code, the scrolled window has not actually been called. However,
      #  we can still get the ID for the scrolledwindow, since getId will assign the scrolledwindow the same
      #  ID durring creation
      self._mainHandler  = mainAppHandler
      self._widgetId     = mainAppHandler.getId("SCROLLEDWINDOW")
      self._widgetHandle = None
      self._buffer       = None

   def Draw(self):
      # Now for the drawing!
      fObj = self._mainHandler.fileHandler.getCurFile()
      if fObj is not None:
         self._buffer = fObj['object']
         self._widgetHandle.maxWidth = fObj['width']
         self._widgetHandle.maxHeight = fObj['height']
         self._widgetHandle.SetScrollbars(
               20 , 20 ,
               fObj['width'] / 20,
               fObj['height'] / 20
            )
      else:
         self._buffer = None

      self.OnPaint()

   def initCanvas(self):
      self._widgetHandle = self._mainHandler.getHandle(self._widgetId)

   def OnPaint(self, event=None):
      dc = wx.PaintDC(self._widgetHandle)
      dc.SetBackground(wx.Brush("Black",wx.SOLID))
      dc.Clear()

      if self._buffer is not None:
         self._widgetHandle.PrepareDC(dc)
         dc.DrawBitmap(self._buffer, 0, 0)
